package CompletableFuture.常用方法;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: thenApply、handle
 * @author: 陈恩涛
 * @create: 2022-08-09 23:12
 **/
public class CfApiDemo2 {
    public static void main(String[] args){
        final ExecutorService threadPool = Executors.newCachedThreadPool();
        CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return 1;
        }, threadPool).handle((f,e)-> {
            int i = 10/0;
            System.out.println("222");
            return f + 2;
        }).handle((f,e) -> {
            int[] arr = {};
            arr[4] = 4;
            System.out.println("333");
            return f + 3;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("最终结算值为：" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });

        System.out.println(Thread.currentThread().getName() + "先去忙其他事情了");
    }
}
